package xby.study.xmlweb.db;

import java.sql.*;

/**
 *
 * 数据库连不上：  注意是机器名！！！！！
 *  1  listener.ora(里面的localhost或127.0.0.1改成机器名)
 *  2  sqlnet.ora (参考下,不用该)
 *  3  tnsnames.ora(里面的localhost或127.0.0.1改成机器名)
 *  https://www.yisu.com/jc/33340.html
 */

public class ConnectionUtil {
    public static void main(String[] args) throws Exception {

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("oracle.jdbc.OracleDriver");


                    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.21.18.152:1521/orcl", "xby", "xby");

                    Statement s = conn.createStatement();
                    String sql = "select 1 from dual";
                    ResultSet result = s.executeQuery(sql);
                    while (result.next()) {
                        int age = result.getInt(1);
                        System.out.println("今年" + age + "岁了！");
                    }
                } catch (ClassNotFoundException | SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                    System.out.println("172.21.158.152 - 连接失败");
                }
            }
        };


        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("oracle.jdbc.OracleDriver");
                    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@8.149.129.60:1521/orcl", "xby", "xby");


                    Statement s = conn.createStatement();
                    String sql = "select 1 from dual";
                    ResultSet result = s.executeQuery(sql);
                    while (result.next()) {
                        int age = result.getInt(1);
                        System.out.println("今年" + age + "岁了！");
                    }
                } catch (ClassNotFoundException | SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                    System.out.println("8.149.129.60 - 连接失败");
                }
            }
        };

        Runnable r3 = new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("oracle.jdbc.OracleDriver");

                    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/orcl", "xby", "xby");

                    Statement s = conn.createStatement();
                    String sql = "select 1 from dual";
                    ResultSet result = s.executeQuery(sql);
                    while (result.next()) {
                        int age = result.getInt(1);
                        System.out.println("今年" + age + "岁了！");
                    }
                } catch (ClassNotFoundException | SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                    System.out.println("localhost - 连接失败");
                }
            }
        };


        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        Thread t3 = new Thread(r3);

        t1.start();
        t2.start();
        t3.start();


    }

    public static void connDatabase() {
        try {
            Class.forName("oracle.jdbc.OracleDriver");


            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.21.158.152:1521/orcl", "xby", "xby");

            Statement s = conn.createStatement();
            String sql = "select 1 from dual";
            ResultSet result = s.executeQuery(sql);
            while (result.next()) {
                int age = result.getInt(1);
                System.out.println("今年" + age + "岁了！");
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
